<?php
/*
*	CentralACL Version 1.1
*	By: Nathan Adams
*
*	Licensed under Apache 2.0 (see LICENSE.txt)
*
*	How to use:
*
*	1. Replace MySQL credentials with your own
*   2. Write the following line at the top of your PHP scripts:
*	include /path/to/centralacl.php;
*   3. ?????
*   4. Profit!
*
*
*/
$conn = new mysqli("localhost", "root", "root", "centralacl");
$IP = $_SERVER['REMOTE_ADDR'];
$hostname = gethostbyaddr($IP);
$tld = "";
$email = "adamsna [at] datanethost.net";
$place = "Datanethost";

function getstr()
{
	try {
		$getstr = "";
		foreach($_GET as $key=>$val)
		{
			$getstr .= $key . " => " . $val . "\n";
		}
	} catch (Exception $e) {
		//pass
	}
	return $getstr;
}

function poststr()
{
	$poststr = "";
	try {
		foreach($_POST as $key=>$val)
		{
			$poststr .= $key . " => " . $val . "\n";
		}
	} catch (Exception $e) {
		
	}
	return $poststr;
}

function logactivity()
{
	global $conn;
	$referer = array_key_exists('HTTP_REFERER', $_SERVER) ? $_SERVER['HTTP_REFERER'] : "";
	$servername = array_key_exists('SERVER_NAME', $_SERVER) ? $_SERVER['SERVER_NAME'] : "";
	$useragent = array_key_exists('HTTP_USER_AGENT', $_SERVER) ? $_SERVER['HTTP_USER_AGENT'] : "";
	$q = "INSERT INTO activity (ip_addr,getdata,postdata,servername,referer,user_agent, datestamp) VALUES ('" . $_SERVER['REMOTE_ADDR'] . "',";
	$q .= "'" . getstr() . "','" . poststr() . "','" . $servername . "',";
	$q .= "'" . $referer . "','" . $useragent . "', " . time() . ")";
	$conn->query($q);
}

if ($hostname)
{
	$hostnamearr = explode(".", $hostname);
	$tld = implode(".",array_splice($hostnamearr, -3, 3));
} else {
	$outtext = "In order to access this site you must have a reverse PTR set for your IP address.<br>";
	$outtext .= "More information: http://en.wikipedia.org/wiki/PTR_Record.\n";
	$outtext .= "If you would like to be added to a whitelist please email $email\n";
	$conn->close();
	die($outtext);
}

$nip = sprintf("%u", ip2long($IP));

//Is the IP on the blocklist?
$whitelistip_check = $conn->query("SELECT * FROM whitelistip WHERE ip_addr = $nip");

if ($whitelistip_check && $whitelistip_check->num_rows == 0)
{
	$blacklistip_check = $conn->query("SELECT * FROM blockedip where start_ip_addr <= $nip AND end_ip_addr >= $nip");
	if ($blacklistip_check && $blacklistip_check->num_rows == 0)
	{
		//Is the hostname on the whitelist?
		$blacklistdomain_check = $conn->query("SELECT * FROM blockddomains WHERE domain LIKE '$tld'");
		if ($blacklistdomain_check && $blacklistdomain_check->num_rows > 0)
		{
			//log activity
			logactivity();
			$outtext = "Your hostname has been banned from $place.<br>";
			$outtext .= "If you believe this to be in error please contact $email";
			$conn->close();
			die($outtext);
		}
	} else {
		//log activity
		logactivity();
		$outtext = "Your IP has been banned from $place.<br>";
		$outtext .= "If you believe this to be in error please contact $email";
		$conn->close();
		die($outtext);
	}
}
$conn->close();
?>